package com.kensure.shike.user.service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import co.kensure.exception.BusinessExceptionUtil;
import co.kensure.exception.ParamUtils;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.ArithmeticUtils;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.MapUtils;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.changshang.model.ChangShang;
import com.kensure.shike.changshang.service.ChangShangService;
import com.kensure.shike.user.dao.SKUserSJYueDao;
import com.kensure.shike.user.model.SKUserSJYue;

/**
 * 用户代金券余额服务实现类
 * 
 * @author fankd created on 2019-10-31
 * @since
 */
@Service
public class SKUserSJYueService extends JSBaseService {

	@Resource
	private SKUserSJYueDao dao;
	@Resource
	private BaseKeyService baseKeyService;
	@Resource
	private ChangShangService changShangService;

	public SKUserSJYue selectOne(Long id) {
		return dao.selectOne(id);
	}

	public List<SKUserSJYue> selectByIds(Collection<Long> ids) {
		return dao.selectByIds(ids);
	}

	public List<SKUserSJYue> selectAll() {
		return dao.selectAll();
	}

	public List<SKUserSJYue> selectByWhere(Map<String, Object> parameters) {
		return dao.selectByWhere(parameters);
	}

	public long selectCount() {
		return dao.selectCount();
	}

	public long selectCountByWhere(Map<String, Object> parameters) {
		return dao.selectCountByWhere(parameters);
	}

	public boolean insert(SKUserSJYue obj) {
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("sk_user_sj_yue"));
		return dao.insert(obj);
	}

	public boolean update(SKUserSJYue obj) {
		super.beforeUpdate(obj);
		return dao.update(obj);
	}

	public boolean updateByMap(Map<String, Object> params) {
		return dao.updateByMap(params);
	}

	public boolean delete(Long id) {
		return dao.delete(id);
	}

	public boolean deleteMulti(Collection<Long> ids) {
		return dao.deleteMulti(ids);
	}

	public boolean deleteByWhere(Map<String, Object> parameters) {
		return dao.deleteByWhere(parameters);
	}

	/**
	 * 根据用户获取他的代金券
	 */
	public List<SKUserSJYue> selectByUserid(Long userid) {
		Map<String, Object> parameters = MapUtils.genMap("userid", userid, "orderby", "id desc");
		List<SKUserSJYue> list = selectByWhere(parameters);
		if(CollectionUtils.isNotEmpty(list)){
			for(SKUserSJYue obj:list){
				ChangShang changshang = changShangService.selectOne(obj.getSjid());
				obj.setChangshang(changshang);
			}
		}
		return list;
	}

	/**
	 * 根据用户、商家获取他的代金券
	 */
	public SKUserSJYue selectByUseridAndSjid(Long userid, Long sjid) {
		Map<String, Object> parameters = MapUtils.genMap("userid", userid, "sjid", sjid);
		List<SKUserSJYue> list = selectByWhere(parameters);
		SKUserSJYue yue = null;
		if (CollectionUtils.isNotEmpty(list)) {
			yue = list.get(0);
		}
		return yue;
	}

	/**
	 * 修改余额
	 * 
	 * @param obj
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public synchronized void updateYue(Long userid, Long sjid, Double yue) {
		ParamUtils.isBlankThrewException(userid, "用户为空");
		ParamUtils.isBlankThrewException(sjid, "商家为空");
		SKUserSJYue sksy = selectByUseridAndSjid(userid, sjid);
		if (sksy == null) {
			sksy = new SKUserSJYue();
			sksy.setYue(0D);
			sksy.setUserid(userid);
			sksy.setSjid(sjid);
			insert(sksy);
		}
		double x = ArithmeticUtils.add(yue, sksy.getYue());
		sksy.setYue(x);
		if (sksy.getYue() < 0) {
			BusinessExceptionUtil.threwException("余额不足");
		}
		update(sksy);
	}

}
